package timing.ukulele.form.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import timing.ukulele.form.persistent.EntityFieldConfigDraft;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.form.mapper.EntityFieldConfigDraftMapper;
import timing.ukulele.persistent.service.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class EntityFieldConfigDraftService extends BaseService<EntityFieldConfigDraftMapper, EntityFieldConfigDraft> {


    /**
     * 分页获取字段属性
     *
     * @param configDraftId 配置表id
     * @param description          描述
     * @param field                字段
     * @param pageNum              当前页码
     * @param pageSize             每页大小
     */
    public ResponseResult<IPage<EntityFieldConfigDraft>> page(Long configDraftId, String description, String field, Integer pageNum, Integer pageSize) {
        Page<EntityFieldConfigDraft> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<EntityFieldConfigDraft> query = new LambdaQueryWrapper<>();
        query.eq(EntityFieldConfigDraft::getEntityConfigDraftId, configDraftId);
        if (!StringUtils.isEmpty(description)) {
            query.like(EntityFieldConfigDraft::getDescription, description);
        }
        if (!StringUtils.isEmpty(field)) {
            query.like(EntityFieldConfigDraft::getField, field);
        }
        query.orderByDesc(EntityFieldConfigDraft::getUpdateTime);
        IPage<EntityFieldConfigDraft> iPage = getBaseMapper().selectPage(page, query);
        return ResponseResult.success(iPage);
    }


    /**
     * 新增，修改草稿属性
     *
     * @param configDraft
     */
    public ResponseResult<EntityFieldConfigDraft> saveData(EntityFieldConfigDraft configDraft) {
        if (configDraft.getId() == null || configDraft.getId() == 0) {
            this.save(configDraft);
        } else {
            this.updateById(configDraft);
        }
        return ResponseResult.success(configDraft);
    }
}
